﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace HelperPrototype._DB
{
    class PhieuDeNghiMuaHangDB
    {
        public static IQueryable selectAll(_DB.DataClasses1DataContext db)
        {
            if (db == null)
            {
                db = new _DB.DataClasses1DataContext();
            }
            var query = from u in db.PhieuDeNghiMuaHangs
                        where u.deleted ==false
                        select u;
            return query;
        }
        public static PhieuDeNghiMuaHang selectID(String id, _DB.DataClasses1DataContext db)
        {
            if (db==null)
            {
                 db = new _DB.DataClasses1DataContext();
            }
           
            var query = from u in db.PhieuDeNghiMuaHangs
                        where u.MaPhieu == id
                        select u ;
            if (query.Count()>0)
            {
                return query.First();
            }
            return null;
            //return query;
        }
        public static bool insert(_DB.PhieuDeNghiMuaHang u ,_DB.DataClasses1DataContext db)
        {
            if (db == null)
            {
                db = new _DB.DataClasses1DataContext();
            }
            db.PhieuDeNghiMuaHangs.InsertOnSubmit(u);
            db.SubmitChanges();
            return true;
        }
        public static void update(PhieuDeNghiMuaHang newPhieu, _DB.DataClasses1DataContext db)
        {
             if (db == null)
            {
                db = new _DB.DataClasses1DataContext();
            }
            var query = (from u in db.PhieuDeNghiMuaHangs
                        where u.MaPhieu == newPhieu.MaPhieu
                        select u);
       //     MessageBox.Show(query.Count().ToString());
            if (query.Count() > 0)
            {
                PhieuDeNghiMuaHang p = query.First();
                p.NguoiTao = newPhieu.NguoiTao;
                p.NguoiDuyet = newPhieu.NguoiDuyet;
                p.NgayTao = newPhieu.NgayTao;
                p.NgayHan = newPhieu.NgayHan;
                p.NoiDung = newPhieu.NoiDung;
                p.LyDoKhongDuyet = newPhieu.LyDoKhongDuyet;
                p.deleted = newPhieu.deleted;
                db.SubmitChanges();
              //  return query.First();
            }
            return ;
            //return query;
        }
        public static void delete(String id,_DB.DataClasses1DataContext db)
        {
            if (db == null)
            {
                db = new _DB.DataClasses1DataContext();
            }
            var query = from u in db.PhieuDeNghiMuaHangs
                        where u.MaPhieu == id
                        select u;
            if (query.Count() > 0)
            {
                PhieuDeNghiMuaHang p = query.First();
                p.deleted = true;
                db.SubmitChanges();
            }
            return ;
            //return query;
        }
    }
    
}
